package eu.otherweb.json;

public class Length {
    private static final float DPI = 72.0f; 
    private static final float m2point = 72 / 0.0254f;
    private static final float m2inch = 0.0254f;
    
    private float length = 0; // meter
	
	public Length(String valueAndUnit) {		
		float value = Float.parseFloat(valueAndUnit.replaceAll("[^0-9\\.]", ""));
		switch (EUnit.valueOf(valueAndUnit.replaceAll("[^a-zA-Z\\.]", "").toUpperCase())) {
            case MM: length = value / 1000; break;
            case CM: length = value / 100; break;
            case M: length = value; break;
            case PT: length = value / m2point; break;
            case PX: length = value / DPI / m2inch; break;
            default: length = 0; break;
        }
		
	}
	
	public float getMeter(){
		return length;
	}
	
	public float getPoint() {
	    return length * m2point;
	}
	
	@Override
	public String toString() {
	    return length + "m";
	}
	
	
	enum EUnit {
	    MM("mm", "meter"),
	    CM("cm", "centimeter"),
	    M("m", "meter"),
	    PT("pt", "point"),
	    PX("px", "pixel");
	    
	    
	    String shortName;
	    String longName;
	    
	    private EUnit(String shortName, String longName) {
            this.shortName = shortName;
            this.longName = longName;
        }
	    
	    @Override
	    public String toString() {
	        return shortName;
	    }
	}
}
